{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# The JOIN operation\n",
    "\n",
    "![rel](https://sqlzoo.net/w/images/c/ce/FootballERD.png)\n",
    "\n",
    "## JOIN and UEFA EURO 2012\n",
    "\n",
    "This tutorial introduces `JOIN` which allows you to use data from two or more tables. The tables contain all matches and goals from UEFA EURO 2012 Football Championship in Poland and Ukraine.\n",
    "\n",
    "The data is available (mysql format) at <http://sqlzoo.net/euro2012.sql>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      " ·········\n"
     ]
    }
   ],
   "source": [
    "# Prerequesites\n",
    "import getpass\n",
    "%load_ext sql\n",
    "pwd = getpass.getpass()\n",
    "# %sql mysql+pymysql://root:$pwd@localhost:3306/sqlzoo\n",
    "%sql postgresql://postgres:$pwd@localhost/sqlzoo\n",
    "%config SqlMagic.displaylimit = 20"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.\n",
    "The first example shows the goal scored by a player with the last name 'Bender'. The `*` says to list all the columns in the table - a shorter way of saying `matchid, teamid, player, gtime`\n",
    "\n",
    "**Modify it to show the matchid and player name for all goals scored by Germany. To identify German players, check for: `teamid = 'GER'`**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/sqlzoo\n",
      "10 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>matchid</th>\n",
       "        <th>player</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>1008</td>\n",
       "        <td>Mario Gómez</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>1010</td>\n",
       "        <td>Mario Gómez</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>1010</td>\n",
       "        <td>Mario Gómez</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>1012</td>\n",
       "        <td>Lukas Podolski</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>1012</td>\n",
       "        <td>Lars Bender</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>1026</td>\n",
       "        <td>Philipp Lahm</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>1026</td>\n",
       "        <td>Sami Khedira</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>1026</td>\n",
       "        <td>Miroslav Klose</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>1026</td>\n",
       "        <td>Marco Reus</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>1030</td>\n",
       "        <td>Mesut Özil</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[(1008, 'Mario Gómez'),\n",
       " (1010, 'Mario Gómez'),\n",
       " (1010, 'Mario Gómez'),\n",
       " (1012, 'Lukas Podolski'),\n",
       " (1012, 'Lars Bender'),\n",
       " (1026, 'Philipp Lahm'),\n",
       " (1026, 'Sami Khedira'),\n",
       " (1026, 'Miroslav Klose'),\n",
       " (1026, 'Marco Reus'),\n",
       " (1030, 'Mesut Özil')]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT matchid, player FROM goal \n",
    "  WHERE teamid = 'GER';"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.\n",
    "\n",
    "From the previous query you can see that Lars Bender's scored a goal in game 1012. Now we want to know what teams were playing in that match.\n",
    "\n",
    "Notice in the that the column `matchid `in the `goal` table corresponds to the `id` column in the `game` table. We can look up information about game 1012 by finding that row in the `game` table.\n",
    "\n",
    "**Show id, stadium, team1, team2 for just game 1012**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/sqlzoo\n",
      "1 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>id</th>\n",
       "        <th>stadium</th>\n",
       "        <th>team1</th>\n",
       "        <th>team2</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>1012</td>\n",
       "        <td>Arena Lviv</td>\n",
       "        <td>DEN</td>\n",
       "        <td>GER</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[(1012, 'Arena Lviv', 'DEN', 'GER')]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT id,stadium,team1,team2\n",
    "  FROM game\n",
    "    WHERE id=1012;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.\n",
    "You can combine the two steps into a single query with a JOIN.\n",
    "\n",
    "```sql\n",
    "SELECT *\n",
    "  FROM game JOIN goal ON (id=matchid)\n",
    "```\n",
    "\n",
    "The **FROM** clause says to merge data from the goal table with that from the game table. The **ON** says how to figure out which rows in **game** go with which rows in **goal** - the **matchid** from **goal** must match **id** from **game**. (If we wanted to be more clear/specific we could say\n",
    "`ON (game.id=goal.matchid)`\n",
    "\n",
    "The code below shows the player (from the goal) and stadium name (from the game table) for every goal scored.\n",
    "\n",
    "**Modify it to show the player, teamid, stadium and mdate for every German goal.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/sqlzoo\n",
      "10 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>player</th>\n",
       "        <th>teamid</th>\n",
       "        <th>stadium</th>\n",
       "        <th>mdate</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Mario Gómez</td>\n",
       "        <td>GER</td>\n",
       "        <td>Arena Lviv</td>\n",
       "        <td>9 June 2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Mario Gómez</td>\n",
       "        <td>GER</td>\n",
       "        <td>Metalist Stadium</td>\n",
       "        <td>13 June 2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Mario Gómez</td>\n",
       "        <td>GER</td>\n",
       "        <td>Metalist Stadium</td>\n",
       "        <td>13 June 2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Lars Bender</td>\n",
       "        <td>GER</td>\n",
       "        <td>Arena Lviv</td>\n",
       "        <td>17 June 2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Lukas Podolski</td>\n",
       "        <td>GER</td>\n",
       "        <td>Arena Lviv</td>\n",
       "        <td>17 June 2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Marco Reus</td>\n",
       "        <td>GER</td>\n",
       "        <td>PGE Arena Gdansk</td>\n",
       "        <td>22 June 2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Miroslav Klose</td>\n",
       "        <td>GER</td>\n",
       "        <td>PGE Arena Gdansk</td>\n",
       "        <td>22 June 2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Sami Khedira</td>\n",
       "        <td>GER</td>\n",
       "        <td>PGE Arena Gdansk</td>\n",
       "        <td>22 June 2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Philipp Lahm</td>\n",
       "        <td>GER</td>\n",
       "        <td>PGE Arena Gdansk</td>\n",
       "        <td>22 June 2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Mesut Özil</td>\n",
       "        <td>GER</td>\n",
       "        <td>National Stadium, Warsaw</td>\n",
       "        <td>28 June 2012</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[('Mario Gómez', 'GER', 'Arena Lviv', '9 June 2012'),\n",
       " ('Mario Gómez', 'GER', 'Metalist Stadium', '13 June 2012'),\n",
       " ('Mario Gómez', 'GER', 'Metalist Stadium', '13 June 2012'),\n",
       " ('Lars Bender', 'GER', 'Arena Lviv', '17 June 2012'),\n",
       " ('Lukas Podolski', 'GER', 'Arena Lviv', '17 June 2012'),\n",
       " ('Marco Reus', 'GER', 'PGE Arena Gdansk', '22 June 2012'),\n",
       " ('Miroslav Klose', 'GER', 'PGE Arena Gdansk', '22 June 2012'),\n",
       " ('Sami Khedira', 'GER', 'PGE Arena Gdansk', '22 June 2012'),\n",
       " ('Philipp Lahm', 'GER', 'PGE Arena Gdansk', '22 June 2012'),\n",
       " ('Mesut Özil', 'GER', 'National Stadium, Warsaw', '28 June 2012')]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT player, teamid, stadium, mdate\n",
    "  FROM game JOIN goal ON (id=matchid)\n",
    "    WHERE goal.teamid = 'GER';"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4.\n",
    "Use the same `JOIN` as in the previous question.\n",
    "\n",
    "**Show the team1, team2 and player for every goal scored by a player called Mario `player LIKE 'Mario%'`**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/sqlzoo\n",
      "9 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>team1</th>\n",
       "        <th>team2</th>\n",
       "        <th>player</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>GER</td>\n",
       "        <td>POR</td>\n",
       "        <td>Mario Gómez</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>NED</td>\n",
       "        <td>GER</td>\n",
       "        <td>Mario Gómez</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>NED</td>\n",
       "        <td>GER</td>\n",
       "        <td>Mario Gómez</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>IRL</td>\n",
       "        <td>CRO</td>\n",
       "        <td>Mario Mandžukic</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>IRL</td>\n",
       "        <td>CRO</td>\n",
       "        <td>Mario Mandžukic</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>ITA</td>\n",
       "        <td>CRO</td>\n",
       "        <td>Mario Mandžukic</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>ITA</td>\n",
       "        <td>IRL</td>\n",
       "        <td>Mario Balotelli</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>GER</td>\n",
       "        <td>ITA</td>\n",
       "        <td>Mario Balotelli</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>GER</td>\n",
       "        <td>ITA</td>\n",
       "        <td>Mario Balotelli</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[('GER', 'POR', 'Mario Gómez'),\n",
       " ('NED', 'GER', 'Mario Gómez'),\n",
       " ('NED', 'GER', 'Mario Gómez'),\n",
       " ('IRL', 'CRO', 'Mario Mandžukic'),\n",
       " ('IRL', 'CRO', 'Mario Mandžukic'),\n",
       " ('ITA', 'CRO', 'Mario Mandžukic'),\n",
       " ('ITA', 'IRL', 'Mario Balotelli'),\n",
       " ('GER', 'ITA', 'Mario Balotelli'),\n",
       " ('GER', 'ITA', 'Mario Balotelli')]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT team1, team2, player \n",
    "  FROM game JOIN goal ON (id=matchid)\n",
    "    WHERE goal.player LIKE 'Mario%';"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.\n",
    "\n",
    "The table `eteam` gives details of every national team including the coach. You can `JOIN` `goal` to `eteam` using the phrase goal `JOIN eteam on teamid=id`\n",
    "\n",
    "**Show `player, teamid, coach, gtime` for all goals scored in the first 10 minutes `gtime<=10`**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/sqlzoo\n",
      "4 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>player</th>\n",
       "        <th>teamid</th>\n",
       "        <th>coach</th>\n",
       "        <th>gtime</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Václav Pilar</td>\n",
       "        <td>CZE</td>\n",
       "        <td>Michal Bílek</td>\n",
       "        <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Petr Jirácek</td>\n",
       "        <td>CZE</td>\n",
       "        <td>Michal Bílek</td>\n",
       "        <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Fernando Torres</td>\n",
       "        <td>ESP</td>\n",
       "        <td>Vicente del Bosque</td>\n",
       "        <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Mario Mandžukic</td>\n",
       "        <td>CRO</td>\n",
       "        <td>Slaven Bilic</td>\n",
       "        <td>3</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[('Václav Pilar', 'CZE', 'Michal Bílek', 6),\n",
       " ('Petr Jirácek', 'CZE', 'Michal Bílek', 3),\n",
       " ('Fernando Torres', 'ESP', 'Vicente del Bosque', 4),\n",
       " ('Mario Mandžukic', 'CRO', 'Slaven Bilic', 3)]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT player, teamid, coach, gtime\n",
    "  FROM goal JOIN eteam ON (teamid=id)\n",
    "    WHERE gtime<=10;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6.\n",
    "\n",
    "To `JOIN` `game` with `eteam` you could use either\n",
    "`game JOIN eteam ON (team1=eteam.id)` or `game JOIN eteam ON (team2=eteam.id)`\n",
    "\n",
    "Notice that because `id` is a column name in both `game` and `eteam` you must specify `eteam.id` instead of just `id`\n",
    "\n",
    "**List the the dates of the matches and the name of the team in which 'Fernando Santos' was the team1 coach.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/sqlzoo\n",
      "2 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>mdate</th>\n",
       "        <th>teamname</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>12 June 2012</td>\n",
       "        <td>Greece</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>16 June 2012</td>\n",
       "        <td>Greece</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[('12 June 2012', 'Greece'), ('16 June 2012', 'Greece')]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT mdate, teamname\n",
    "  FROM game JOIN eteam ON (team1=eteam.id)\n",
    "    WHERE eteam.coach = 'Fernando Santos';"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 7.\n",
    "\n",
    "**List the player for every goal scored in a game where the stadium was 'National Stadium, Warsaw'**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/sqlzoo\n",
      "9 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>player</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Robert Lewandowski</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Dimitris Salpingidis</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Alan Dzagoev</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Jakub Blaszczykowski</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Giorgos Karagounis</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Cristiano Ronaldo</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Mario Balotelli</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Mario Balotelli</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Mesut Özil</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[('Robert Lewandowski',),\n",
       " ('Dimitris Salpingidis',),\n",
       " ('Alan Dzagoev',),\n",
       " ('Jakub Blaszczykowski',),\n",
       " ('Giorgos Karagounis',),\n",
       " ('Cristiano Ronaldo',),\n",
       " ('Mario Balotelli',),\n",
       " ('Mario Balotelli',),\n",
       " ('Mesut Özil',)]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT player\n",
    "  FROM goal JOIN game ON (matchid=id)\n",
    "    WHERE stadium = 'National Stadium, Warsaw';"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 8. More difficult questions\n",
    "\n",
    "The example query shows all goals scored in the Germany-Greece quarterfinal.\n",
    "**Instead show the name of all players who scored a goal against Germany.**\n",
    "\n",
    "> __HINT__   \n",
    "> Select goals scored only by non-German players in matches where GER was the id of either **team1** or **team2**.\n",
    "> You can use `teamid!='GER'` to prevent listing German players.\n",
    "> You can use `DISTINCT` to stop players being listed twice."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/sqlzoo\n",
      "5 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>player</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Dimitris Salpingidis</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Georgios Samaras</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Mario Balotelli</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Michael Krohn-Dehli</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Robin van Persie</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[('Dimitris Salpingidis',),\n",
       " ('Georgios Samaras',),\n",
       " ('Mario Balotelli',),\n",
       " ('Michael Krohn-Dehli',),\n",
       " ('Robin van Persie',)]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT DISTINCT player\n",
    "  FROM game JOIN goal ON matchid = id \n",
    "    WHERE (team1='GER' OR team2='GER') AND goal.teamid <> 'GER';"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 9.\n",
    "Show teamname and the total number of goals scored.\n",
    "\n",
    "> __COUNT and GROUP BY__  \n",
    "> You should COUNT(*) in the SELECT line and GROUP BY teamname"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/sqlzoo\n",
      "16 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>teamname</th>\n",
       "        <th>goals</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Croatia</td>\n",
       "        <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Czech Republic</td>\n",
       "        <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Denmark</td>\n",
       "        <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>England</td>\n",
       "        <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>France</td>\n",
       "        <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Germany</td>\n",
       "        <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Greece</td>\n",
       "        <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Italy</td>\n",
       "        <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Netherlands</td>\n",
       "        <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Poland</td>\n",
       "        <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Portugal</td>\n",
       "        <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Republic of Ireland</td>\n",
       "        <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Russia</td>\n",
       "        <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Spain</td>\n",
       "        <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Sweden</td>\n",
       "        <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Ukraine</td>\n",
       "        <td>2</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[('Croatia', 4),\n",
       " ('Czech Republic', 4),\n",
       " ('Denmark', 4),\n",
       " ('England', 5),\n",
       " ('France', 3),\n",
       " ('Germany', 10),\n",
       " ('Greece', 5),\n",
       " ('Italy', 6),\n",
       " ('Netherlands', 2),\n",
       " ('Poland', 2),\n",
       " ('Portugal', 6),\n",
       " ('Republic of Ireland', 1),\n",
       " ('Russia', 5),\n",
       " ('Spain', 12),\n",
       " ('Sweden', 5),\n",
       " ('Ukraine', 2)]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT teamname, COUNT(player) AS goals\n",
    "  FROM eteam JOIN goal ON id=teamid\n",
    "    GROUP BY teamname\n",
    "    ORDER BY teamname;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 10.\n",
    "\n",
    "**Show the stadium and the number of goals scored in each stadium.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/sqlzoo\n",
      "8 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>stadium</th>\n",
       "        <th>goals</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Stadion Miejski (Wroclaw)</td>\n",
       "        <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Stadion Miejski (Poznan)</td>\n",
       "        <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>PGE Arena Gdansk</td>\n",
       "        <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Donbass Arena</td>\n",
       "        <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Olimpiyskiy National Sports Complex</td>\n",
       "        <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Arena Lviv</td>\n",
       "        <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Metalist Stadium</td>\n",
       "        <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>National Stadium, Warsaw</td>\n",
       "        <td>9</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[('Stadion Miejski (Wroclaw)', 9),\n",
       " ('Stadion Miejski (Poznan)', 8),\n",
       " ('PGE Arena Gdansk', 13),\n",
       " ('Donbass Arena', 7),\n",
       " ('Olimpiyskiy National Sports Complex', 14),\n",
       " ('Arena Lviv', 9),\n",
       " ('Metalist Stadium', 7),\n",
       " ('National Stadium, Warsaw', 9)]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT stadium, COUNT(player) AS goals\n",
    "  FROM game JOIN goal ON id=matchid\n",
    "    GROUP BY stadium;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 11.\n",
    "**For every match involving 'POL', show the matchid, date and the number of goals scored.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/sqlzoo\n",
      "3 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>matchid</th>\n",
       "        <th>mdate</th>\n",
       "        <th>goals</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>1001</td>\n",
       "        <td>8 June 2012</td>\n",
       "        <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>1004</td>\n",
       "        <td>12 June 2012</td>\n",
       "        <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>1005</td>\n",
       "        <td>16 June 2012</td>\n",
       "        <td>1</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[(1001, '8 June 2012', 2),\n",
       " (1004, '12 June 2012', 2),\n",
       " (1005, '16 June 2012', 1)]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT matchid, mdate, COUNT(player) AS goals\n",
    "  FROM game JOIN goal ON matchid = id \n",
    "    WHERE (team1 = 'POL' OR team2 = 'POL')\n",
    "    GROUP BY matchid, mdate;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 12.\n",
    "**For every match where 'GER' scored, show matchid, match date and the number of goals scored by 'GER'**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/sqlzoo\n",
      "5 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>matchid</th>\n",
       "        <th>mdate</th>\n",
       "        <th>goals</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>1008</td>\n",
       "        <td>9 June 2012</td>\n",
       "        <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>1010</td>\n",
       "        <td>13 June 2012</td>\n",
       "        <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>1030</td>\n",
       "        <td>28 June 2012</td>\n",
       "        <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>1026</td>\n",
       "        <td>22 June 2012</td>\n",
       "        <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>1012</td>\n",
       "        <td>17 June 2012</td>\n",
       "        <td>2</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[(1008, '9 June 2012', 1),\n",
       " (1010, '13 June 2012', 2),\n",
       " (1030, '28 June 2012', 1),\n",
       " (1026, '22 June 2012', 4),\n",
       " (1012, '17 June 2012', 2)]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT matchid, mdate, COUNT(player) AS goals \n",
    "  FROM game JOIN goal ON id=matchid\n",
    "    WHERE teamid = 'GER'\n",
    "    GROUP BY matchid, mdate;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 13.\n",
    "List every match with the goals scored by each team as shown. This will use \"CASE WHEN\" which has not been explained in any previous exercises.\n",
    "\n",
    "  mdate | team1 | score1 | team2 | score2\n",
    "--------|-------|--------|-------|----------\n",
    "1 July 2012 | ESP | 4 | ITA | 0\n",
    "10 June 2012 | ESP | 1 | ITA | 1\n",
    "10 June 2012 | IRL | 1 | CRO | 3\n",
    "... | ... | ... | ... | ... | ...\n",
    "\n",
    "Notice in the query given every goal is listed. If it was a team1 goal then a 1 appears in score1, otherwise there is a 0. You could SUM this column to get a count of the goals scored by team1. **Sort your result by mdate, matchid, team1 and team2.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/sqlzoo\n",
      "31 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>mdate</th>\n",
       "        <th>team1</th>\n",
       "        <th>score1</th>\n",
       "        <th>team2</th>\n",
       "        <th>score2</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>1 July 2012</td>\n",
       "        <td>ESP</td>\n",
       "        <td>4</td>\n",
       "        <td>ITA</td>\n",
       "        <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>10 June 2012</td>\n",
       "        <td>ESP</td>\n",
       "        <td>1</td>\n",
       "        <td>ITA</td>\n",
       "        <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>10 June 2012</td>\n",
       "        <td>IRL</td>\n",
       "        <td>1</td>\n",
       "        <td>CRO</td>\n",
       "        <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>11 June 2012</td>\n",
       "        <td>FRA</td>\n",
       "        <td>1</td>\n",
       "        <td>ENG</td>\n",
       "        <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>11 June 2012</td>\n",
       "        <td>UKR</td>\n",
       "        <td>2</td>\n",
       "        <td>SWE</td>\n",
       "        <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>12 June 2012</td>\n",
       "        <td>GRE</td>\n",
       "        <td>1</td>\n",
       "        <td>CZE</td>\n",
       "        <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>12 June 2012</td>\n",
       "        <td>POL</td>\n",
       "        <td>1</td>\n",
       "        <td>RUS</td>\n",
       "        <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>13 June 2012</td>\n",
       "        <td>DEN</td>\n",
       "        <td>2</td>\n",
       "        <td>POR</td>\n",
       "        <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>13 June 2012</td>\n",
       "        <td>NED</td>\n",
       "        <td>1</td>\n",
       "        <td>GER</td>\n",
       "        <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>14 June 2012</td>\n",
       "        <td>ITA</td>\n",
       "        <td>1</td>\n",
       "        <td>CRO</td>\n",
       "        <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>14 June 2012</td>\n",
       "        <td>ESP</td>\n",
       "        <td>4</td>\n",
       "        <td>IRL</td>\n",
       "        <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>15 June 2012</td>\n",
       "        <td>UKR</td>\n",
       "        <td>0</td>\n",
       "        <td>FRA</td>\n",
       "        <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>15 June 2012</td>\n",
       "        <td>SWE</td>\n",
       "        <td>2</td>\n",
       "        <td>ENG</td>\n",
       "        <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>16 June 2012</td>\n",
       "        <td>CZE</td>\n",
       "        <td>1</td>\n",
       "        <td>POL</td>\n",
       "        <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>16 June 2012</td>\n",
       "        <td>GRE</td>\n",
       "        <td>1</td>\n",
       "        <td>RUS</td>\n",
       "        <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>17 June 2012</td>\n",
       "        <td>POR</td>\n",
       "        <td>2</td>\n",
       "        <td>NED</td>\n",
       "        <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>17 June 2012</td>\n",
       "        <td>DEN</td>\n",
       "        <td>1</td>\n",
       "        <td>GER</td>\n",
       "        <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>18 June 2012</td>\n",
       "        <td>CRO</td>\n",
       "        <td>0</td>\n",
       "        <td>ESP</td>\n",
       "        <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>18 June 2012</td>\n",
       "        <td>ITA</td>\n",
       "        <td>2</td>\n",
       "        <td>IRL</td>\n",
       "        <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>19 June 2012</td>\n",
       "        <td>ENG</td>\n",
       "        <td>1</td>\n",
       "        <td>UKR</td>\n",
       "        <td>0</td>\n",
       "    </tr>\n",
       "</table>\n",
       "<span style=\"font-style:italic;text-align:center;\">31 rows, truncated to displaylimit of 20</span>"
      ],
      "text/plain": [
       "[('1 July 2012', 'ESP', 4, 'ITA', 0),\n",
       " ('10 June 2012', 'ESP', 1, 'ITA', 1),\n",
       " ('10 June 2012', 'IRL', 1, 'CRO', 3),\n",
       " ('11 June 2012', 'FRA', 1, 'ENG', 1),\n",
       " ('11 June 2012', 'UKR', 2, 'SWE', 1),\n",
       " ('12 June 2012', 'GRE', 1, 'CZE', 2),\n",
       " ('12 June 2012', 'POL', 1, 'RUS', 1),\n",
       " ('13 June 2012', 'DEN', 2, 'POR', 3),\n",
       " ('13 June 2012', 'NED', 1, 'GER', 2),\n",
       " ('14 June 2012', 'ITA', 1, 'CRO', 1),\n",
       " ('14 June 2012', 'ESP', 4, 'IRL', 0),\n",
       " ('15 June 2012', 'UKR', 0, 'FRA', 2),\n",
       " ('15 June 2012', 'SWE', 2, 'ENG', 3),\n",
       " ('16 June 2012', 'CZE', 1, 'POL', 0),\n",
       " ('16 June 2012', 'GRE', 1, 'RUS', 0),\n",
       " ('17 June 2012', 'POR', 2, 'NED', 1),\n",
       " ('17 June 2012', 'DEN', 1, 'GER', 2),\n",
       " ('18 June 2012', 'CRO', 0, 'ESP', 1),\n",
       " ('18 June 2012', 'ITA', 2, 'IRL', 0),\n",
       " ('19 June 2012', 'ENG', 1, 'UKR', 0),\n",
       " ('19 June 2012', 'SWE', 2, 'FRA', 0),\n",
       " ('21 June 2012', 'CZE', 0, 'POR', 1),\n",
       " ('22 June 2012', 'GER', 4, 'GRE', 2),\n",
       " ('23 June 2012', 'ESP', 2, 'FRA', 0),\n",
       " ('24 June 2012', 'ENG', 0, 'ITA', 0),\n",
       " ('27 June 2012', 'POR', 0, 'ESP', 0),\n",
       " ('28 June 2012', 'GER', 1, 'ITA', 2),\n",
       " ('8 June 2012', 'POL', 1, 'GRE', 1),\n",
       " ('8 June 2012', 'RUS', 4, 'CZE', 1),\n",
       " ('9 June 2012', 'NED', 0, 'DEN', 1),\n",
       " ('9 June 2012', 'GER', 1, 'POR', 0)]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT mdate, team1, SUM(CASE WHEN teamid=team1 THEN 1 ELSE 0 END) AS score1, team2,\n",
    "  SUM(CASE WHEN teamid=team2 THEN 1 ELSE 0 END) AS score2\n",
    "    FROM game LEFT JOIN goal ON matchid = id\n",
    "    GROUP BY mdate, team1, team2, matchid\n",
    "    ORDER BY mdate, matchid, team1, team2;"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
